home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: RAND_MAX
- Date: 1 Apr 1996 06:55:43 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4joqpfINNg2s@keats.ugrad.cs.ubc.ca>
- References: <4jnr55$e6l@skivs.ski.org>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4jnr55$e6l@skivs.ski.org>, Gemini Thunder <gt@ns.oon.or.jp> wrote:
- >I have a (stupid) question / observation about RAND_MAX.
- >
- >K&R2 says:
- > "rand returns a a pseudo-random integer in the range 0 to RAND_MAX,
- >which is at least 32767"
- >
- >It looks like RAND_MAX is not required to be the same as any other
- >limit (such as MAX_INT, etc.), that is fine, but why?
- >Wouldn't it be easier if RAND_MAX == MAX_INT or some other value?
- >
- >The reason I say this is how can you be sure your array will hold a
- >RAND_MAX sized integer, without some checking before hand, if RAND_MAX
- >is out there doing its own thing?
-
- If you look at the prototype for rand(), the answer to that is obvious. It
- returns int, therefore you can store these numbers in arrays of int. The return
- type of rand() gives you the type of object in which you can store the result.
-
- Presumably, the reason RAND_MAX is distinct from INT_MAX (not MAX_INT!!!) is
- to allow rand() the freedom to return a smaller range, say a few bits less than
- the size of an integer, which is still _stored_ in an int type.
-
- >Or does ANSI pin it down (in which case why have a RAND_MAX #defined),
- >or am I just worked up over nothing and overlooking something obvious?
-
- It pins it down in the #define for RAND_MAX. Often, RAND_MAX is just
-
- #define RAND_MAX INT_MAX
-
- but it need not be so, hence the separate symbolic constants.
- --
-
-